using System.Collections;
using Lucene.Net.Index;
using Lucene.Net.Search;

namespace LuceneSearch.ContactApp.Searching
{
    public class RowSecurityFilter : Filter
    {
        private readonly int[] accessIDs;

        public RowSecurityFilter(int[] accessIDs)
        {
            this.accessIDs = accessIDs;
        }

        public override BitArray Bits(IndexReader reader)
        {
            var bits = new BitArray(reader.MaxDoc());

            var docs = new int[1];
            var freq = new int[1];

            foreach (var id in accessIDs)
            {
                var termDocs = reader.TermDocs(new Term("Id", id.ToString()));
                var count = termDocs.Read(docs, freq);

                bits.Set(docs[0], count == 1);
            }

            return bits;
        }
    }
}